﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.ComponentModel.DataAnnotations;
using WebSimplicity.Data.Helpers;
using System.Web.Security;

namespace WebSimplicity.Data
{

    public interface IWebsite
    {
        Guid WebsiteId { get; set; }
        Guid AccountId { get; set; }
        [Display(Name="Website Name")]
        string WebsiteName { get; set; }
        string SystemSubDomain { get; set; }
        Guid Theme { get; set; }
        int FrontPage { get; set; }
    }
    [MetadataType(typeof(IWebsite))]
    partial class Website : IWebsite {

        public Website Current()
        {
            Entities db = new Entities(Config.GetConnection("Data.Model"));
            string BaseDomainName = ConfigurationManager.AppSettings["BaseDomainName"];
            string hostname = HttpContext.Current.Request.Url.Host;

            if (hostname == "localhost") { hostname = BaseDomainName; }
            if (hostname.StartsWith("www.")) { hostname = hostname.Substring(4); }
            Domain ws;
            if (db.Domains.Any(i => i.DomainName == hostname))
            {
                ws = db.Domains.Single(i => i.DomainName == hostname);
                db.Connection.Close();
                return ws.ws_Websites;
            }
            else if (hostname.IndexOf("." + BaseDomainName) == hostname.Length - ("." + BaseDomainName).Length && hostname.IndexOf("." + BaseDomainName) > -1)
            {
                string subdomain = hostname.Remove(hostname.Length - ("." + BaseDomainName).Length);
                if (db.Websites.Any(i => i.SystemSubDomain == subdomain))
                {
                    return db.Websites.Single(i => i.SystemSubDomain == subdomain);
                }
                else
                {
                    return null;
                }
            }
            else if (hostname == BaseDomainName)
            {
                return db.Websites.Single(i => i.WebsiteId == new Guid());
            }
            else
            {
                return null;
            }
        }

        public Website Create()
        {
            Entities db = new Entities(Config.GetConnection("Data.Model"));
            MembershipUser user = Membership.GetUser();
            if (db.Websites.Count() == 0)
            {
                this.WebsiteId = new Guid();
                this.SystemSubDomain = "www";
            }
            else
            {
                this.WebsiteId = Guid.NewGuid();
            }
            this.FrontPage = 0;
            db.Websites.AddObject(this);
            db.SaveChanges();
            var page = new ContentPage
            {
                WebsiteId = this.WebsiteId,
                Title = "Temporary Homepage",
                MenuTitle = "Home",
                PageGuid = Guid.NewGuid(),
                SortOrder = 0,
                ParentId = 0,
                Slug = "/",
                Template = "index",
                PublishDate = DateTime.Now,
                Published = true,
                CreatedBy = (Guid)user.ProviderUserKey,
                CreatedOn = DateTime.Now,
                ModifiedBy = (Guid)user.ProviderUserKey,
                ModifiedOn = DateTime.Now
            };
            db.ContentPages.AddObject(page);
            this.FrontPage = page.PageId;
            db.SaveChanges();
            return this;
        }
    }

   
}
